//
//  NetworkTools.swift
//  CSYMicroBlockSina
//
//  Created by 姚彦兆 on 15/11/15.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD
enum AFNSESSION_TYPE : NSInteger{

    case GET = 0
    
    case POST = 1
    
}

class NetworkTools: AFHTTPSessionManager {
    
    //定义单例对象
    static let shareNetWorkTools : NetworkTools = {
    
        let urlStr = "https://api.weibo.com/"
        
        let baseURL = NSURL(string: urlStr)
        
        let tools = NetworkTools(baseURL: baseURL)
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
    
        return tools
        
    }()
    
    func requiredJsonValueToDictionary( requestType : AFNSESSION_TYPE , urlStr : String , parameters : [String : AnyObject]? , finishedBlock : (result : [String : AnyObject]? , error : NSError?) -> ()){
    
        //判断请求方式
        if requestType == AFNSESSION_TYPE.GET{
            
            GET(urlStr, parameters: parameters, success: { (_, result) -> Void in
                
                //获取到正确的信息
                if let dataArray = result as? [String : AnyObject]{
                
                    finishedBlock(result: dataArray, error: nil)
                
                    return
                
                }
                
                finishedBlock(result: nil, error: dataError)
                
                
                }) { (_, error) -> Void in
                    
                    finishedBlock(result: nil, error: error)
                    
            }
        
        }else if requestType == AFNSESSION_TYPE.POST{
        
        
            POST(urlStr, parameters: parameters, success: { (_, result) -> Void in
                
                if let dict = result as? [String : AnyObject]{
                    
                    finishedBlock(result: dict, error: nil)
                    
                    return
                    
                }
                
                finishedBlock(result: nil, error: dataError)
                
                }) { (_, error) -> Void in
                    
                    finishedBlock(result: nil, error: error)
                    
            }
        
        }else{
        
            return
        
        }
    
    }
    //定义上传的方法,拼接请求头信息
    
    func uploadPicture(imageData : NSData , urlStr : String , parameters : [String : AnyObject]? , finishedBlock : (result : [String : AnyObject]? , error : NSError?) -> ()){
    
        POST(urlStr, parameters: parameters, constructingBodyWithBlock: { (multipartFormData) -> Void in
            
            //在这里构建请求头信息
            multipartFormData.appendPartWithFileData(imageData, name: "pic", fileName: "myPic", mimeType: "image/jpeg")
            
            }, success: { (_, result) -> Void in
                
                finishedBlock(result: result as? [String : AnyObject], error: nil)
                
            }) { (_, error) -> Void in
                
                finishedBlock(result: nil, error: error)
                
        }
        
    }
    
}
















